package com.xcm.controller;

import com.alibaba.fastjson.JSONObject;
import com.xcm.cache.RedisCacheDao;
import com.xcm.config.CustomProperties;
import com.xcm.constant.ResponseConstant;
import com.xcm.exception.ControllerException;
import com.xcm.exception.ServiceException;
import com.xcm.model.JsonResponse;
import com.xcm.model.TbDeviceModel;
import com.xcm.model.vo.DeviceModelListVO;
import com.xcm.page.PageInfo;
import com.xcm.service.DeviceModelService;
import com.xcm.util.GetUserUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * 终端型号Controller
 */
@Controller
@RequestMapping(value = "/deviceModel")
public class DeviceModelController {
	private static Logger logger = LoggerFactory.getLogger(DeviceModelController.class);
	@Autowired
	private DeviceModelService deviceModelService;
	@Autowired
	private RedisCacheDao redisCacheDao;
	@Autowired
	private CustomProperties customProperties;

	/**
	 * 获取终端型号列表（返回JSON格式）
	 */
	@ResponseBody
	@RequestMapping(value = "/getDeviceModeList")
	public Object getDeviceModeList() {
		try {
			return deviceModelService.listDeviceModels();
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("DeviceModelController getDeviceModeList 获取终端型号列表失败：" + e.getMessage());
			return new ControllerException("获取终端型号列表失败");
		}
	}

	/**
	 * 获取终端型号分页列表
	 */
	@RequestMapping(value = "/listPage")
	public String listPage(Integer pageNum, Integer pageSize, Model model) {
		PageInfo<DeviceModelListVO> pageInfo = deviceModelService.listDeviceModelByPage(pageNum, pageSize);
		model.addAttribute("deviceModelList", pageInfo.getList());
		model.addAttribute("pageNum", pageInfo.getPageNum());
		model.addAttribute("pageSize", pageInfo.getPageSize());
		model.addAttribute("total", pageInfo.getTotal());
		return "deviceModel_list";
	}

	/**
	 * 保存终端型号
	 */
	@ResponseBody
	@RequestMapping(value = "/save")
	public Object saveDeviceModel(TbDeviceModel deviceModel, HttpServletRequest request) {
		//获取用户信息
		JSONObject user = null;
		try {
			user = GetUserUtils.getUser(request, redisCacheDao, customProperties);
		} catch (ServiceException e) {
			e.printStackTrace();
			logger.error("DeviceModelController saveDeviceModel 保存终端型号失败：" + e.getMessage());
			return new JsonResponse(ResponseConstant.CODE_FAIL,"保存终端型号失败");
		}
		if(user == null) {
			return new JsonResponse(ResponseConstant.CODE_FAIL,"用户名为空，请联系管理员");
		}
		String model = deviceModel.getDeviceModel();
		if (StringUtils.isBlank(model)) {
			return new JsonResponse(ResponseConstant.CODE_FAIL, "输入参数不能为空");
		}
		try {
			deviceModelService.saveDeviceModel(deviceModel, user, request);
		} catch (ServiceException e) {
			e.printStackTrace();
			logger.error("DeviceModelController saveDeviceModel 保存终端型号失败：" + e.getMessage());
			return new JsonResponse(ResponseConstant.CODE_FAIL, e.getMessage());
		}
		return "";
	}

	/**
	 * 修改终端型号
	 */
	@ResponseBody
	@RequestMapping(value = "/update")
	public Object updateDeviceModel(TbDeviceModel deviceModel, HttpServletRequest request) {
		//获取用户信息
		JSONObject user = null;
		try {
			user = GetUserUtils.getUser(request, redisCacheDao, customProperties);
		} catch (ServiceException e) {
			e.printStackTrace();
			logger.error("DeviceModelController updateDeviceModel 修改终端型号失败：" + e.getMessage());
			return new JsonResponse(ResponseConstant.CODE_FAIL,"修改终端型号失败");
		}
		if(user == null) {
			return new JsonResponse(ResponseConstant.CODE_FAIL,"用户名为空，请联系管理员");
		}
		String model = deviceModel.getDeviceModel();
		if (deviceModel.getDeviceModelId() == null) {
			return new JsonResponse(ResponseConstant.CODE_FAIL, "ID不能为空");
		}
		if (StringUtils.isBlank(model)) {
			return new JsonResponse(ResponseConstant.CODE_FAIL, "输入参数不能为空");
		}
		try {
			deviceModelService.updateDeviceModel(deviceModel, user, request);
		} catch (ServiceException e) {
			e.printStackTrace();
			logger.error("DeviceModelController updateDeviceModel 修改终端型号失败：" + e.getMessage());
			return new JsonResponse(ResponseConstant.CODE_FAIL,"修改终端型号失败");
		}
		return "";
	}

	/**
	 * 通过ID删除终端型号
	 *
	 * @param deviceModelIds 终端型号ID
	 */
	@ResponseBody
	@RequestMapping(value = "/delete")
	public Object deleteDeviceModel(String deviceModelIds, HttpServletRequest request) {
		//获取用户信息
		JSONObject user = null;
		try {
			user = GetUserUtils.getUser(request, redisCacheDao, customProperties);
		} catch (ServiceException e) {
			e.printStackTrace();
			logger.error("DeviceModelController deleteDeviceModel 删除终端型号失败：" + e.getMessage());
			return new ControllerException("删除终端型号失败");
		}
		if(user == null) {
			return new ControllerException("用户名为空，请联系管理员");
		}
		if (StringUtils.isBlank(deviceModelIds)) {
			return new JsonResponse(ResponseConstant.CODE_FAIL, "ID不能为空");
		}
		try {
			deviceModelService.deleteDeviceModelBatch(deviceModelIds, user, request);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("DeviceModelController deleteDeviceModel 删除终端型号失败：" + e.getMessage());
			return new ControllerException("删除终端型号失败");
		}
		return "";
	}
}
